home *** CD-ROM | disk | FTP | other *** search
/ PC Direct 1998 August / PC Direct August 1998.iso / S / powerj / Product / hpp.z / WQUERYDR.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  17.6 KB  |  549 lines

  1. /*************************************************************************
  2.  *
  3.  * WODBCQuery -- Low-level Query interface for ODBC.
  4.  *
  5.  *************************************************************************/
  6.  
  7. #ifndef _WQUERYDR_HPP_INCLUDED
  8. #define _WQUERYDR_HPP_INCLUDED
  9.  
  10. #ifndef _WVECTOR_HPP_INCLUDED
  11. #include "wvector.hpp"
  12. #endif
  13. #ifndef _WREFOBJ_HPP_INCLUDED
  14. #include "wrefobj.hpp"
  15. #endif
  16. #ifndef _WQUERY_HPP_INCLUDED
  17. #include "wquery.hpp"
  18. #endif
  19. #ifndef _WSQLSTMT_HPP_INCLUDED
  20. #include "wsqlstmt.hpp"
  21. #endif
  22.  
  23. class WODBCDriver;
  24. class WODBCTransaction;
  25. class WODBCQueryColumnInfo;
  26. class WCriticalSection;
  27.  
  28. extern template WVector<WQuery>;
  29.  
  30. //
  31. // WBaseQuery
  32. //
  33. // Skeleton class for implementing real query drivers
  34. //
  35.  
  36. class WCMCLASS WBaseQuery : public WQuery {
  37.  
  38.     WDeclareSubclass( WBaseQuery, WQuery );
  39.  
  40.     public:
  41.  
  42.         WBaseQuery( WQuery *proxy=NULL );
  43.  
  44.         ~WBaseQuery();
  45.  
  46.     public:
  47.  
  48.         /****************************************************************
  49.          * Overrides
  50.          ****************************************************************/
  51.  
  52.         // AlwaysFetchOptions
  53.  
  54.         WBool GetAlwaysFetchOptions() const;
  55.         WBool SetAlwaysFetchOptions( WBool always );
  56.  
  57.         // AlwaysFetchRowPosition
  58.  
  59.         WBool GetAlwaysFetchRowPosition() const;
  60.         WBool SetAlwaysFetchRowPosition( WBool always );
  61.  
  62.         // AlwaysMoveCursor
  63.  
  64.         WBool GetAlwaysMoveCursor() const;
  65.         WBool SetAlwaysMoveCursor( WBool always );
  66.  
  67.         // AutoPrepare
  68.  
  69.         WBool GetAutoPrepare() const;
  70.         WBool SetAutoPrepare( WBool on );
  71.  
  72.         // BindLimit
  73.  
  74.         WULong GetBindLimit() const;
  75.         WBool  SetBindLimit( WULong limit );
  76.  
  77.         // BindPolicy
  78.  
  79.         WQueryBindPolicy GetBindPolicy() const;
  80.         WBool            SetBindPolicy( WQueryBindPolicy policy );
  81.  
  82.         // CurrentRow
  83.  
  84.         WLong GetCurrentRow() const;
  85.  
  86.         // DisplayErrorDialog
  87.  
  88.         WBool GetDisplayErrorDialog() const;
  89.         WBool SetDisplayErrorDialog( WBool display );
  90.  
  91.         // DisplayWarningDialog
  92.  
  93.         WBool GetDisplayWarningDialog() const;
  94.         WBool SetDisplayWarningDialog( WBool display );
  95.  
  96.         WICursorProxyBase *GetICursorProxy() const;
  97.         WBool              SetICursorProxy( WICursorProxyBase *p );
  98.  
  99.         // LongColumnBindSize
  100.  
  101.         WULong GetLongColumnBindSize() const;
  102.         WBool  SetLongColumnBindSize( WULong size );
  103.  
  104.         // SQL
  105.  
  106.         WString GetSQL() const;
  107.         WBool   SetSQL( const WString & str );
  108.  
  109.         // State
  110.         
  111.         WDWord GetState() const;
  112.  
  113.         WBool GetUseDefaultBindTypes() const;
  114.         WBool SetUseDefaultBindTypes( WBool use );
  115.  
  116.         WLong Reference();
  117.         WLong Unreference();
  118.  
  119.         /****************************************************************
  120.          * Item Properties
  121.          ****************************************************************/
  122.  
  123.         // NumericOption
  124.  
  125.         WLong GetNumericOption( WULong id, WBool *ok=NULL ) const;
  126.         WBool SetNumericOption( WULong id, WLong value );
  127.  
  128.         /****************************************************************
  129.          * Internal
  130.          ****************************************************************/
  131.  
  132.     public:
  133.  
  134.         WBool CopySettingsToDriver( WBool assumeDefaults );
  135.         WBool AddClone( WQuery *clone );
  136.         WBool RemoveClone( WQuery *clone );
  137.  
  138.         WBool GetThreadSafe() const;
  139.         WBool SetThreadSafe( WBool safe );
  140.  
  141.         WBool ThreadLock( WBool wait=TRUE, WDWord time=0xFFFFFFFF );
  142.  
  143.         WBool ThreadUnlock();
  144.  
  145.     protected:
  146.  
  147.         WString GetTableNameFromStatement() const;
  148.         virtual void ResetState( WBool requery=FALSE );
  149.  
  150.         WQuery             *_proxy;
  151.         WString             _sqlStatement;
  152.         WDWord              _state;
  153.         WBool               _autoPrepare;
  154.         WLong               _rowsetSize;
  155.         WLong               _keysetSize;
  156.         WQueryCursorType    _cursorType;
  157.         WString             _cursorName;
  158.         WBool               _alwaysGetOptions;
  159.         WQueryBindPolicy    _bindPolicy;
  160.         WULong              _bindLimit;
  161.         WULong              _longColumnBindSize;
  162.         WBool               _alwaysFetchRowPosition;
  163.         WBool               _alwaysMoveCursor;
  164.         WBool               _displayErrors;
  165.         WBool               _displayWarnings;
  166.         WBool               _traceToLog;
  167.         WULong              _traceLevel;
  168.         WULong              _maxRows;
  169.         WULong              _queryTimeout;
  170.         WQueryUpdatePolicy  _updatePolicy;
  171.         WBool               _useDefaultBindTypes;
  172.         WBool               _isRequery;
  173.         WBool               _fetchAbsoluteOK;
  174.  
  175.         //
  176.         // The actual policy being use by the query object as
  177.         // _updatePolicy can be WQUPAuto.
  178.         //
  179.         WQueryUpdatePolicy  _updatePolicyInUse;
  180.         WBool               _isClone;
  181.         WQuery             *_cloneMaster;
  182.         WVector<WQuery>     _cloneList;
  183.         WLong               _refCount;
  184.         WICursorProxyBase  *_qiProxy;
  185.  
  186.         WCriticalSection   *_critSect;
  187.         wllist_header       _states;
  188.  
  189.         #ifdef _DEBUG
  190.  
  191.         class WCMCLASS QTrace {
  192.             public:
  193.                 QTrace( const WBaseQuery *, const WChar * );
  194.                 ~QTrace();
  195.  
  196.                 const WBaseQuery *query;
  197.                 const WChar      *function;
  198.         };
  199.  
  200.         friend class QTrace;
  201.  
  202.         void QDbg( const QTrace *q, const WChar *format, ... ) const;
  203.  
  204.         #endif
  205. };
  206.  
  207. //
  208. // WODBCQuery
  209. //
  210. // Implements the query interface for ODBC.
  211. //
  212.  
  213. class WCMCLASS WODBCQuery : public WBaseQuery {
  214.  
  215.     WDeclareSubclass( WODBCQuery, WBaseQuery );
  216.  
  217.     public:
  218.  
  219.         WODBCQuery( WQuery *proxy=NULL );
  220.  
  221.         virtual ~WODBCQuery();
  222.  
  223.         /***************************************************************
  224.          * Properties
  225.          ***************************************************************/
  226.  
  227.         /****************************************************************
  228.          * Overrides
  229.          ****************************************************************/
  230.  
  231.     public:
  232.  
  233.         WBool GetAutoEdit() const;
  234.         WBool SetAutoEdit( WBool autoEdit );
  235.  
  236.         WBool GetAutoRefresh() const;
  237.         WBool SetAutoRefresh( WBool autoRefresh );
  238.  
  239.         WBool GetBOF() const;
  240.  
  241.         WDWord GetBookmark() const;
  242.  
  243.         WQueryConcurrencyLevel GetConcurrencyLevel() const;
  244.         WBool                  SetConcurrencyLevel( WQueryConcurrencyLevel l );
  245.  
  246.         WString GetCursorName() const;
  247.         WBool   SetCursorName( const WString & str );
  248.  
  249.         WQueryCursorType GetCursorType() const;
  250.         WBool            SetCursorType( WQueryCursorType type );
  251.  
  252.         WBool GetDriverPacksRows() const;
  253.         WBool SetDriverPacksRows( WBool packsRows );
  254.  
  255.         WBool GetEOF() const;
  256.  
  257.         WDWord GetHandle() const;
  258.         WBool  SetHandle( WDWord h );
  259.  
  260.         WTransaction *GetTransactionObject() const;
  261.         WBool         SetTransactionObject( WTransaction *obj );
  262.  
  263.         const WDataColumn & GetColumn( WShort index ) const;
  264.         const WDataColumn & GetColumn( const WString & name ) const;
  265.  
  266.         WShort GetColumnCount() const;
  267.  
  268.         WShort GetColumnIndex( const WString & str ) const;
  269.  
  270.         WLong GetCurrentOffset() const;
  271.  
  272.         WDSEditMode GetEditMode() const;
  273.  
  274.         WBool GetEmpty() const;
  275.  
  276.         WLong GetErrorCode( WLong *apiCode=NULL ) const;
  277.  
  278.         WDataErrorArray GetErrorList() const;
  279.  
  280.         WBool GetErrorInfo( WString *errorMessage,
  281.                             WString *state=NULL,
  282.                             WLong *nativeErrorCode=NULL ) const;
  283.  
  284.         WLong GetFetchedRows() const;
  285.  
  286.         WLong GetKeysetSize() const;
  287.         WBool SetKeysetSize( WLong size );
  288.  
  289.         WULong GetMaxLength() const;
  290.         WBool  SetMaxLength( WULong maxLength );
  291.  
  292.         WULong GetMaxRows() const;
  293.         WBool  SetMaxRows( WULong maxRows );
  294.  
  295.         WBool GetNull() const;
  296.  
  297.         WDataValue GetParameter( WShort index ) const;
  298.         WBool      SetParameter( WShort index,
  299.                                  const WDataValue & val,
  300.                                  WColumnDataType sqlType=SQL_TYPE_NULL,
  301.                                  WQueryParameterType type=WQPTInput,
  302.                                  SDWORD maxSize=0 );
  303.  
  304.         WULong GetQueryTimeout() const;
  305.         WBool  SetQueryTimeout( WULong seconds );
  306.  
  307.         WBool GetRawData( WShort index, WNativeDataType type,
  308.                           void *buffer, WLong bufferSize=0,
  309.                           WLong *bytesRemaining=NULL ) const;
  310.  
  311.         WBool GetReadOnly() const;
  312.  
  313.         WBool GetRetrieveData() const;
  314.         WBool SetRetrieveData( WBool ret );
  315.  
  316.         WBool GetRowTargetChanged() const;
  317.         WBool SetRowTargetChanged( WBool changed );
  318.  
  319.         WBool GetRowChanged() const;
  320.         WBool SetRowChanged( WBool changed );
  321.  
  322.         WLong  GetRowCount( WBool forceCount=TRUE ) const;
  323.  
  324.         WLong  GetRowsetSize() const;
  325.         WBool  SetRowsetSize( WLong size );
  326.  
  327.         UWORD GetRowsetStatus( WLong row=1 ) const;
  328.  
  329.         WBool GetTargetsEnabled() const;
  330.         WBool SetTargetsEnabled( WBool enabled );
  331.  
  332.         WBool GetAlwaysReadWriteTargets() const;
  333.         WBool SetAlwaysReadWriteTargets( WBool enabled );
  334.  
  335.         WBool GetTraceToLog() const;
  336.         WBool SetTraceToLog( WBool on );
  337.  
  338.         WQueryUpdatePolicy GetUpdatePolicy() const;
  339.         WBool              SetUpdatePolicy( WQueryUpdatePolicy policy );
  340.  
  341.         WBool GetUseBookmarks() const;
  342.         WBool SetUseBookmarks( WBool use );
  343.  
  344.         WBool Add( WBool copyValues=FALSE, WBool append=FALSE,
  345.                    WBool copyIntoBuffer=FALSE );
  346.  
  347.         WBool AddSearchParameter( WShort columnIndex, WDataValue value,
  348.                       WStmtCondition=WSNAnd,WStmtCompare compare=WSMLike);
  349.         WBool AddSearchParameter( const WString & columnName, WDataValue value,
  350.                       WStmtCondition=WSNAnd,WStmtCompare compare=WSMLike);
  351.  
  352.         WBool BindColumn( WShort column, WNativeDataType type=SQL_C_DEFAULT,
  353.                           void *arrayStart=NULL, SDWORD elementSize=0,
  354.                           SDWORD *lengthArray=NULL );
  355.  
  356.         WBool BindParameter( WShort column, WQueryParameterType type,
  357.                              WNativeDataType cType,
  358.                              WColumnDataType sqlType,
  359.                              UDWORD precision, SWORD scale,
  360.                              void *dataBuffer, SDWORD maxSize,
  361.                              SDWORD *actualLength );
  362.  
  363.         WBool BoundControlSearch(WStmtCondition=WSNAnd,
  364.                       WStmtCompare compare=WSMLike);
  365.  
  366.         WBool CancelUpdate( WBool notifyTargets=TRUE );
  367.  
  368.         WBool  Close( WBool closeCursor=TRUE, WBool r=FALSE );
  369.  
  370.         WBool  Create( WTransaction *trans );
  371.  
  372.         WBool ClearTargets();
  373.  
  374.         WQuery *Clone();
  375.  
  376.         WBool Delete( WBool triggerEvent=TRUE, WBool notifyTargets=TRUE );
  377.  
  378.         WBool DeleteSearchParameters();
  379.  
  380.         WBool  Destroy();
  381.  
  382.         WBool Edit();
  383.  
  384.         WBool  Execute( const WString & str=WString::GetNullString() );
  385.  
  386.         WBool  Fetch( WLong row, WBool notifyTargets=TRUE,
  387.                       WDSMoveType type=WDSMoveAbsolute, WBool trigger=TRUE );
  388.  
  389.         WBool FetchErrors( WLong errorCode, WLong funcCode );
  390.  
  391.         WBool  FlushUnboundValues();
  392.  
  393.         WBool  Open( WBool e=TRUE, WBool r=FALSE );
  394.  
  395.         WBool  Prepare();
  396.  
  397.         WBool MarkColumnForUpdate( WShort column, WBool mark );
  398.  
  399.         WBool MoreResults();
  400.  
  401.         WBool  Move( WLong row, WBool notifyTargets=TRUE,
  402.                      WDSMoveType type=WDSMoveAbsolute, WBool trigger=TRUE );
  403.  
  404.         WBool Refresh();
  405.  
  406.         WBool RefreshTargets( WBool multiRow=FALSE );
  407.  
  408.         WBool ResetSearch();
  409.  
  410.         WBool Resubmit( WBool executeStatement=TRUE, WBool closeCursor=TRUE );
  411.  
  412.         WBool Search();
  413.         WBool Search( WShort columnIndex, WDataValue value,
  414.                       WStmtCondition=WSNAnd,WStmtCompare compare=WSMLike);
  415.         WBool Search( const WString & columnName, WDataValue value,
  416.                       WStmtCondition=WSNAnd,WStmtCompare compare=WSMLike);
  417.  
  418.         WBool Update( WBool triggerEvent=TRUE, WBool notifyTargets=TRUE );
  419.  
  420.         WDataValue GetValue( WShort index, WNativeDataType type=SQL_C_DEFAULT ) const;
  421.         WBool      SetValue( WShort index, const WDataValue & val );
  422.  
  423.         WLong GetNumericOption( WULong id, WBool *ok=NULL ) const;
  424.         WBool SetNumericOption( WULong id, WLong value );
  425.  
  426.         WBool QueryColumnBinding( WShort column,
  427.                                   WNativeDataType *boundAs=NULL,
  428.                                   void **arrayStart=NULL,
  429.                                   SDWORD *elementSize=NULL,
  430.                                   SDWORD **lengthArray=NULL );
  431.  
  432.         WBool UnbindColumn( WShort column );
  433.  
  434.         WBool UnbindParameter( WShort index );
  435.  
  436.         WBool Connect( WEventData *data=NULL );
  437.         WBool Disconnect( WEventData *data=NULL );
  438.  
  439.         /****************************************************************
  440.          * Internal
  441.          ****************************************************************/
  442.  
  443.     public:
  444.  
  445.         WBool CopySettingsToDriver( WBool assumeDefaults );
  446.         WBool TransactionEvent( WEventID id, WEventData *data=NULL );
  447.         WBool CountRows( WBool ensureValid=FALSE );
  448.         WBool SetSearchValue( WShort index, const WDataValue & val );
  449.  
  450.     protected:
  451.  
  452.         WBool BuildSearch(WStmtCondition condition,
  453.                       WStmtCompare compare);
  454.         WBool Check( RETCODE retcode, int func=0 ) const;
  455.         void  InternalError( WInt err=0 );
  456.         WBool PrepareForFetch( WBool bind=TRUE ) const;
  457.         WBool AllocateColumnInfo();
  458.         WBool CallEvent( WEventID id, WEventData *data=NULL );
  459.         WBool AutoBind();
  460.         void  LoadColumnTargets();
  461.         WBool SyncDataValues();
  462.         WBool DetachDataValues();
  463.         void  AdjustEventMask();
  464.         WBool DoUpdate();
  465.         WLong GetBoundColumns() const;
  466.         void  NotifyTargets( WLong row, WULong reason,
  467.                              WULong action = WDataActionNone,
  468.                              WBool multiValueOnly = FALSE ) const;
  469.  
  470.         WBool GetDriverOption( WULong id, WLong & num, WBool def ) const;
  471.         WBool GetDriverOption( WULong id, WString & str, WBool def ) const;
  472.         WBool SetDriverOption( WULong id, WLong num, WBool def );
  473.         WBool SetDriverOption( WULong id, const WString & str, WBool def );
  474.         WBool RefreshRowset();
  475.         WBool DoParameterBinding();
  476.         WBool DoParameterSyncing();
  477.         WBool LateBindTarget( WQuery *query, WDataTarget *target );
  478.         WBool PerformCursorUpdate( const WULong action );
  479.         void  EventNotice( WEventID id, WEventNotice type );
  480.         void  ResetState( WBool requery=FALSE );
  481.  
  482.         WBool RestoreState( const WDataSourceState state, WBool restore=TRUE );
  483.         WBool SaveState( WDataSourceState & state );
  484.  
  485.         WQueryUpdatePolicy TryOtherUP() const;
  486.  
  487.     protected:
  488.  
  489.         WTransaction           *_transDriver;
  490.         WODBCTransaction       *_realTransDriver;
  491.         WODBCDriver            *_driver;
  492.         WODBCStmtHandle         _handle;
  493.         WBool                   _freeStmt;
  494.         WULong                  _maxLength;
  495.         WQueryConcurrencyLevel  _concurrencyLevel;
  496.         WDSEditMode             _editMode;
  497.         WBool                   _rowCountValid;
  498.         WBool                   _retrieveData;
  499.         WBool                   _useBookmarks;
  500.         WBool                   _autoRefresh;
  501.         WBool                   _autoEdit;
  502.         WBool                   _fetchedError;
  503.         WLong                   _lastCode;      
  504.         WLong                   _lastFunc;
  505.         WDataErrorArray         _errorList;
  506.         WDWord                  _eventMask;
  507.  
  508.         // Column info
  509.  
  510.         SWORD                   _numCols;
  511.         WDataColumn            *_columnList;
  512.         WODBCQueryColumnInfo  **_columnInfo;
  513.         WShort                  _lastVisited;
  514.  
  515.         // Parameter info
  516.  
  517.         wllist_header           _paramList;
  518.         WBool                   _bindParams;
  519.         WBool                   _paramsBound;
  520.  
  521.         // Rowset info
  522.  
  523.         UDWORD                  _numRows;         // number of valid rows
  524.         SDWORD                  _cursorRow;       // current row in rowset
  525.         SDWORD                  _firstRow;        // row # of first row in set
  526.         SDWORD                  _actualCursorRow;
  527.         UWORD                  *_rowStatusList;
  528.         WBool                   _bof;
  529.         WBool                   _eof;
  530.         WBool                   _needToSync;
  531.         WLong                   _rowCount;        // total # of rows
  532.         WLong                   _deltaRows;       // # add/del'd rows in rowset
  533.         // Relative move required to set the current row after fetching
  534.         // backwards
  535.         WLong                   _rowOffset;
  536.         WBool                   _addingInRowset;
  537.         WLong                   _deltaRowsInRowset;
  538.         WBool                   _driverPacksRows;
  539.  
  540.         // Search info
  541.         WSQLStatement           _wSqlStmt;
  542.         WShort                  _initParms; // user's initial number of parms
  543.         WShort                  _noOfParms; // final number of parms after
  544.                                             // incrementing
  545.  
  546. };
  547.  
  548. #endif
  549.